package org.linlinjava.litemall.core.notify;

import com.github.qcloudsms.SmsSenderUtil;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.github.qcloudsms.httpclient.HTTPException;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.common.profile.Language;
import com.tencentcloudapi.cvm.v20170312.CvmClient;
import com.tencentcloudapi.cvm.v20170312.models.DescribeInstancesResponse;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
import com.tencentcloudapi.sms.v20210111.models.SendStatus;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;


import java.io.IOException;

/*
 * 腾讯云短信服务
 */
public class TencentSmsSender implements SmsSender {
    private final Log logger = LogFactory.getLog(TencentSmsSender.class);

    private SmsSingleSender sender;


    private int appid;
    private String appkey;
    private String appSecret;
    private String sign;

    public Log getLogger() {
        return logger;
    }

    public void setAppid(int appid) {
        this.appid = appid;
    }


    public void setAppkey(String appkey) {
        this.appkey = appkey;
    }


    public void setAppSecret(String appSecret) {
        this.appSecret = appSecret;
    }

    public String getSign() {
        return sign;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }

    @Override
    public SmsResult send(String phone, String content) {
        return null;
    }

    @Override
    public SmsResult sendWithTemplate(String phone, String templateId, String[] params) {
        SmsResult smsResult = new SmsResult();
        try {
            Credential cred = new Credential(this.appkey, this.appSecret);
            // 实例化一个http选项，可选的，没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("sms.tencentcloudapi.com");
            // 实例化一个client选项，可选的，没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            SmsClient client = new SmsClient(cred, "ap-beijing", clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            SendSmsRequest req = new SendSmsRequest();
            String[] phoneNumberSet1 = {"86" + phone};
            req.setPhoneNumberSet(phoneNumberSet1);

            req.setSmsSdkAppId(String.valueOf(this.appid));
            req.setTemplateId(templateId);
            req.setSignName(this.sign);
            req.setTemplateParamSet(params);

            // 返回的resp是一个SendSmsResponse的实例，与请求对象对应
            SendSmsResponse resp = client.SendSms(req);
            SendStatus[] sendStatusSet = resp.getSendStatusSet();
            SendStatus sendStatus = sendStatusSet[0];
            if ("Ok".equals(sendStatus)) {
                smsResult.setSuccessful(true);
            } else {
                smsResult.setSuccessful(false);
            }
            // 输出json格式的字符串回包

        } catch (TencentCloudSDKException e) {
            smsResult.setSuccessful(false);
        }
        return smsResult;
    }


}
